package cz.tpj.database.aktivita_zajimavost;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;

import cz.tpj.database.DatabaseConnector;
import cz.tpj.util.ErrorDatabase;

public class TableActivityInterests {
	public static final String TABLE       		 = "aktivita_zaujimavost";
    public static final String ID          		 = "id";
    public static final String ID_ACTIVITY 		 = "aktivity_id";
    public static final String ID_INTEREST_PLACE = "bod_zaujmu_id";
    
    private static TableActivityInterests im = null;
    private Connection con;
    private Statement stmt = null;

    public static TableActivityInterests getInstance(){
        if(im == null){
            im = new TableActivityInterests();
        }
        return im;
    }
    //=======================metody-------------------------
    private void databaseConnect() throws SQLException{
        DatabaseConnector dc = DatabaseConnector.getInstance();
        con = dc.getConnection();
        stmt = con.createStatement();
    }
    
    public String addAllActivitiesToInterest(String idActivity[], String idInterest) {
    	String message = "";
    	try{
    	for(int i=0; i<idActivity.length; i++){
    		addActivityToInterest(idActivity[i], idInterest);
    	}
    	}catch (ErrorDatabase e) {
			message = e.getMessage();
		}
    	return message;
    }
    
    public void addActivityToInterest(String idActivity, String idInterest) throws ErrorDatabase{
        try {
            databaseConnect();
            String insert = "INSERT INTO "+TABLE+" ("+ID_ACTIVITY+", "+ID_INTEREST_PLACE+") " +
            		"VALUES ('"+idActivity+"','"+idInterest+"');";
            stmt.executeUpdate(insert);
            con.commit();
        } catch (SQLException ex) {
        	throw new ErrorDatabase(ErrorDatabase.ERROR_DATABASE_CONECTION, "Cbyba pri praci s databazovym serverem." + ex.getMessage());
        } finally{
            try {
                stmt.close();
            } catch (Exception ex) {
            }

        }
    }
    
    public void deleteActivitiesIfDeletedInterest(String idInterestPlace) throws ErrorDatabase{
    	try{
    		databaseConnect();
            String execute = "DELETE FROM "+TABLE+" WHERE "+ID_INTEREST_PLACE+" = "+idInterestPlace+";";
            stmt.executeUpdate(execute);
            con.commit();
    	}catch (SQLException ex) {
    		throw new ErrorDatabase(ErrorDatabase.ERROR_DATABASE_CONECTION, "Cbyba pri praci s databazovym serverem." + ex.getMessage());
		}finally{
            try {
                stmt.close();
            } catch (Exception ex) {
            }
        }
    }
    
    public String[] getActivitiesOfInterestPlace(String interestId) throws ErrorDatabase{
    	ResultSet rs = null;
    	String[] result = new String[0];
    	//
    	try {
            databaseConnect();
            String query = "SELECT a.nazov FROM aktivity a, aktivita_zaujimavost z WHERE a.id = z.aktivity_id AND z.bod_zaujmu_id = "+interestId+";";
            rs = stmt.executeQuery(query);
            
            ArrayList<String> res = new ArrayList<String>();
            while(rs.next()){
                res.add(rs.getString(1));
            }
            //
            if(res.size() > 0){
            	result = new String[res.size()];
            	Iterator<String> it = res.iterator();
            	int i=0;
            	while(it.hasNext()){
            		result[i] = it.next();
            		i++;
            	}	
            }
            	
        } catch (SQLException ex) {
        	throw new ErrorDatabase(ErrorDatabase.ERROR_DATABASE_CONECTION, "Cbyba pri praci s databazovym serverem." + ex.getMessage());
        } finally{
            try {
                rs.close();
                stmt.close();
            } catch (Exception ex) {
            }

        }
    	
    	return result;
    }
}
